/*
 * IJA2011, Board game "Clovece nezlob se"
 * grp108
 * Tomáš Mičiak,xmicia00
 * Andrej Martinák, xmarti51
 *
 */

package src.clovece.game;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 *  Class representing figurine
 */
public final class Figurine extends JLabel implements MouseListener {
    private GameManagement gameManagement = GameManagement.getGameManagementInstance();
    private GameInfo gameInfo = GameInfo.getGameInfoInstance();
    private Player player = null;
    private int actualPosition = -1;
    private int startingPosition;
    private int number = -1;
    private boolean gameCompleted = false;
    private boolean canMove = false;
    private int gameSet;
    private int figurine;
    private int counter = 0;

    public Figurine(int gameSet, int figurine, int number, Player p) {
        this.player = p;
        this.number = number;
        this.gameSet = gameSet;
        this.figurine = figurine;
        ImageIcon icon = new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                  + "lib" + System.getProperty("file.separator") + "img"
                  + System.getProperty("file.separator") + "figurine" + gameSet + figurine + "a" + ".png");
        this.setIcon(icon);

        if(this.gameInfo.getGameBoardSize() == 36) {
            if(this.player.getPosition() == 0) {
                this.setStartingPosition(27);
            }
            else if(this.player.getPosition() == 1) {
                this.setStartingPosition(9);
            }
            else if(this.player.getPosition() == 2) {
                this.setStartingPosition(0);
            }
            else if(this.player.getPosition() == 3) {
                this.setStartingPosition(18);
            }
        }
        else if(this.gameInfo.getGameBoardSize() == 48) {
            if(this.player.getPosition() == 0) {
                this.setStartingPosition(37);
            }
            else if(this.player.getPosition() == 1) {
                this.setStartingPosition(13);
            }
            else if(this.player.getPosition() == 2) {
                this.setStartingPosition(1);
            }
            else if(this.player.getPosition() == 3) {
                this.setStartingPosition(25);
            }
        }
        else {
            if(this.player.getPosition() == 0) {
                this.setStartingPosition(46);
            }
            else if(this.player.getPosition() == 1) {
                this.setStartingPosition(16);
            }
            else if(this.player.getPosition() == 2) {
                this.setStartingPosition(1);
            }
            else if(this.player.getPosition() == 3) {
                this.setStartingPosition(31);
            }
        }
    }

    public void select() {
        ImageIcon icon = new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                  + "lib" + System.getProperty("file.separator") + "img"
                  + System.getProperty("file.separator") + "figurine" + this.gameSet + this.figurine + "b" + ".png");
        this.setIcon(icon);
        this.repaint();
    }

    public void deselect() {
        ImageIcon icon = new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                  + "lib" + System.getProperty("file.separator") + "img"
                  + System.getProperty("file.separator") + "figurine" + this.gameSet + this.figurine + "a" + ".png");
        this.setIcon(icon);
        this.repaint();
    }

    public void moveFigurine(int x, int y) {
        this.setLocation(x, y);
        this.deselect();
    }

    public void setCounter(int f) {
        this.counter = f;
    }

    public int getCounter() {
        return this.counter;
    }

    public void setFigurine(int f) {
        this.figurine = f;
    }

    public int getFigurine() {
        return this.figurine;
    }

    public void setNumber(int f) {
        this.number = f;
    }

    public int getNumber() {
        return this.number;
    }
    
    public void setGameSet(int set) {
        this.gameSet = set;
    }

    public int getGameSet() {
        return this.gameSet;
    }

    public void setActualPosition(int p) {
        this.actualPosition = p;
    }
    
    public int getActualPosition() {
        return this.actualPosition;
    }

    public void setStartingPosition(int p) {
        this.startingPosition = p;
    }

    public int getStartingPosition() {
        return this.startingPosition;
    }

    public void setGameCompleted(boolean c) {
        this.gameCompleted = c;
    }

    public boolean getGameCompleted() {
        return this.gameCompleted;
    }

    public void setCanMove(boolean c) {
        this.canMove = c;
    }

    public boolean getCanMove() {
        return this.canMove;
    }

    public Player getPlayer() {
        return this.player;
    }

    public void mouseClicked(MouseEvent e) {
        if(this.gameInfo.getYourPosition() == this.gameInfo.getPlayerOnTurn() && this.gameInfo.getPlayerOnTurn() == this.player.getPosition()) {
            this.gameManagement.selectFigurine(this);
        }
        else if(this.gameInfo.getLocalGame()) this.gameManagement.deselectFigurine();
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
    
}
